﻿var muserrole = {
    //列表初始化
    gridInit: function () {
        $("#muserrole-grid").datagrid({
            url: "/manage/userrole/grid",
            nowrap: false, border: false,
            fit: true, singleSelect: true,
            loadMsg: "正在处理。。。",
            idField: "ID", toolbar: "#muserrole-tools",
            columns:
            [[
                {
                    field: "RoleID", title: "角色", width: 228, align: "center",
                    formatter: function (value, row, index) {
                        if (row.BasicRole) { return row.BasicRole.Name; }
                    }
                }
            ]]
        });
    },

    //添加
    insert: function () {
        var row = $("#muser-grid").datagrid("getSelected");
        if (row) {
            muserrole.mutual("添加", null, row.ID);
        }
        else { manage.slide("请选择用户!"); }
    },
    //修改
    update: function () {
        var row = $("#muserrole-grid").datagrid("getSelected");
        if (row) {
            muserrole.mutual("修改", row.ID, null);
        }
        else { manage.slide(manage.nullRowMsg); }
    },
    //删除
    remove: function () {
        var row = $("#muserrole-grid").datagrid("getSelected");
        if (row) {
            manage.confirm("确认删除?", function (del) {
                if (del) {
                    manage.ajax({
                        url: "/manage/userrole/remove",
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            manage.slide(result.message);
                            if (result.success) { muserrole.reload(); }
                        }
                    });
                }
            });
        }
        else { manage.slide(manage.delNullRowMsg); }
    },
    //实体操作
    mutual: function (title, id, userid) {
        var href = "/manage/userrole/page?" + $.param({ id: id, userid: userid });
        var d = manage.dialog({
            title: title, href: href, width: 640,
            handler: function () {
                var rolerow = $("#muserrole-rolegrid").datagrid("getSelected");
                if (rolerow) {
                    $("#muserrole-roleid").val(rolerow.ID);
                }
                else { manage.msg("请选择赋予角色!"); return; }
                manage.form({
                    dialog: d, formid: "#muserrole-model",
                    url: "/manage/userrole/save",
                    success: function (result) { muserrole.reload(); }
                });
            },
            onLoad: function () { muserrole.formInit(); }
        });
    },
    //弹窗初始化
    formInit: function () {
        $("#muserrole-rolegrid").datagrid({
            url: "/manage/role/allgrid",
            nowrap: false, rownumbers: true, title: "选择角色",
            fit: true, singleSelect: true, idField: "ID",
            columns:
            [[
                { field: "Name", title: "名称", width: 140, align: "center" },
                { field: "Remark", title: "描述", width: 325, align: "center" },
                {
                    field: "IsEnabled", title: "启用", width: 40, align: "center",
                    formatter: function (value, row, index) { return manage.iconYesOrNo(value); }
                }
            ]],
            onLoadSuccess: function (data) {
                var roleid = $("#muserrole-roleid").val();
                if (roleid) {
                    $("#muserrole-rolegrid").datagrid("selectRecord", roleid);
                }
            }
        });
    },

    //刷新列表
    reload: function () {
        $("#muserrole-grid").datagrid("unselectAll");
        $("#muserrole-grid").datagrid("reload");
    }
};

$(function () { muserrole.gridInit(); });